<template>
	<view>
		<view class="top-tabber-wrapper">
			<view class="item" :class="{ check: check_tabber_type == item.type }" v-for="(item, index) in top_tabber" :key="index"
			 @click="cliTabberItem(item)">
				<text>{{ item.name }}</text>
			</view>
		</view>
		<view class="order-list">
			<view class="order-item" v-for="(item, index) in order_list" :key="index" @click="$db._NavTo('order-food-detail-page', { order_id: item.order_id })">
				<view class="goods-info">
					<image :src="item.cover" mode=""></image>
					<view class="info">
						<text class="name">{{ item.dish_name }}</text>
						<view class="price-and-type">
							<text class="price">{{ item.dish_type }} {{ item.plan_name }} {{ item.type_name }}</text>
							<text class="type" v-if="item.dishes_status === 1">待发货</text>
							<text class="type" v-else-if="item.dishes_status === 2">待收货</text>
							<text class="type" v-else-if="item.dishes_status === 3">待评价</text>
							<text class="type" v-else-if="item.dishes_status === 4">已完成</text>
							<text class="type" v-else-if="item.dishes_status === 5">已取消</text>
						</view>
					</view>
				</view>
				<view class="time-and-btn">
					<text class="time">预约时间：{{ item.dish_type === '午餐' ? item.lunch_time : item.dinner_time }}</text>
					<text class="btn" v-if="item.dishes_status === 2" @click.stop="cliTakeDelivery(item.order_id)">确认收货</text>
					<text class="btn" v-else-if="item.dishes_status === 3" @click.stop="$db._NavTo('comment', { order_id: item.order_id , type: 'food'})">去评价</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				top_tabber: [{
						name: '全部',
						type: '0'
					},
					{
						name: '待发货',
						type: '1'
					},
					{
						name: '待收货',
						type: '2'
					},
					{
						name: '待评价',
						type: '3'
					},
					{
						name: '已完成',
						type: '4'
					}
				],
				check_tabber_type: 0,
				order_list: [],
				order_list_page: 1,
				is_ajax: false
			};
		},
		onShow() {
			// 初始化分页
			this.order_list_page = 1;
			this.getData();
		},
		onReachBottom() {
			console.log('sss');
			this.getData();
		},
		methods: {
			getData: function() {
				if (this.is_ajax) {
					return false;
				}
				this.is_ajax = true;
				this.$api.getCycliceDish({
						type: 2,
						ship_status: this.check_tabber_type,
						limit: 10,
						page: this.order_list_page
					})
					.then(res => {
						this.is_ajax = false;
						if (this.order_list_page === 1) {
							window.scrollTo(0, 0);
							this.order_list = res.data.orders;
						} else {
							this.order_list.push.apply(this.order_list, res.data.orders);
						}
						this.order_list_page++;
						if (res.data.orders.length < 1) {
							this.$db._ShowToast('没有更多订单啦')
							return false;
						}
					});
			},
			cliTabberItem: function(item) {
				// tabber切换
				this.check_tabber_type = item.type;
				// 初始化分页
				this.order_list_page = 1;
				this.getData();
			},
			cliTakeDelivery: function(order_id) {
				// 确认收货
				this.$api.updateOrderStatus({
						order_id
					})
					.then(res => {
						this.$db._ShowToast(res.msg, () => {
							// 初始化分页
							this.order_list_page = 1;
							this.getData();
						})
					})
			}
		}
	};
</script>

<style lang="scss">
	page {
		background: #ffffff;
		padding-top: 100rpx;
		overflow: hidden;
	}

	.top-tabber-wrapper {
		position: fixed;
		z-index: 3;
		left: 0;
		top: 0;
		box-sizing: border-box;
		padding: 0 38rpx;
		width: 100%;
		height: 100rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		background: #ffffff;

		.item {
			width: 100rpx;
			height: 100rpx;
			flex: 0 0 100rpx;
			text-align: center;
			line-height: 100rpx;
			border-bottom: 2rpx solid #fff;

			text {
				font-size: 28rpx;
				color: #5c5c5b;
			}

			&.check {
				border-bottom: 2rpx solid #c19101;

				text {
					color: rgba(193, 145, 1, 1);
				}
			}
		}
	}

	.order-list {
		position: relative;
		z-index: 2;

		.order-item {
			padding: 34rpx 20rpx;
			margin: 0 28rpx;
			border-bottom: 2px solid rgba(242, 242, 242, 1);

			.goods-info {
				padding-bottom: 40rpx;
				display: flex;

				image {
					flex: 0 0 138rpx;
					width: 138rpx;
					height: 138rpx;
					border-radius: 10rpx;
				}

				.info {
					flex: 0 0 470rpx;
					padding-left: 22rpx;
					display: flex;
					flex-flow: column;

					.name {
						font-size: 32rpx;
						color: rgba(37, 37, 37, 1);
					}

					.price-and-type {
						margin-top: auto;
						display: flex;

						.price {
							font-size: 28rpx;
							color: #252525;
						}

						.type {
							margin-left: auto;
							font-size: 22rpx;
							color: #c19101;
						}
					}
				}
			}

			.time-and-btn {
				display: flex;
				align-items: center;

				.time {
					font-size: 22rpx;
					color: #313130;
				}

				.btn {
					margin-left: auto;
					margin-right: 20rpx;
					padding: 10rpx 30rpx;
					font-size: 22rpx;
					color: #494949;
					border: 1rpx solid rgba(157, 157, 157, 1);
					border-radius: 10rpx;
				}
			}
		}
	}
</style>
